package com.stylefeng.guns.rest.common.aop;

import com.stylefeng.guns.common.bo.WebuserBo;
import com.stylefeng.guns.common.constant.state.LogSucceed;
import com.stylefeng.guns.common.constant.state.LogType;
import com.stylefeng.guns.common.persistence.model.OperationLog;
import com.stylefeng.guns.common.util.WebUserKit;
import com.stylefeng.guns.core.support.RestConstant;
import com.stylefeng.guns.rest.common.accesslog.AsyncAccessLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 日志
 */
@Aspect
@Component
@Slf4j
@Order(10000)//优先级非常低
public class BusinessLogAop {

    @Autowired
    private AsyncAccessLog asyncAccessLog;

//    @Pointcut(value = "execution(* com.stylefeng.guns.rest.modular.controller..*.*(..)) " +
//            "&& !execution(* com.stylefeng.guns.rest.modular.controller.UserNoAuthController.*(..))" +
//            "&& !execution(* com.stylefeng.guns.rest.modular.controller.acct.NotifyController.*(..))")
@Pointcut(value = "execution(* com.stylefeng.guns.rest.modular.controller..*.*(..)) " +
		"&& !execution(* com.stylefeng.guns.rest.modular.controller.UserNoAuthController.*(..))")
    public void controllerAspect() {
    }

    private static final String LOG_NAME_DESC = "rest";

    @Around("controllerAspect()")
    public Object interceptor(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        String method = proceedingJoinPoint.getSignature().toShortString();
        WebuserBo webuserBo = WebUserKit.getWebuserBo();
        HttpServletRequest request = WebUserKit.getHttpRequest();

        String accessSource = request.getHeader(RestConstant.ACCESS_SOURCE);
        OperationLog optLog = new OperationLog();
        {
            optLog.setUserid(webuserBo == null ? 0 : webuserBo.getId());//防止出现在未登录的情况下,这种情况暂时不存在
            optLog.setClassname(method);
            optLog.setCreatetime(DateTime.now().toDate());
            optLog.setSucceed(LogSucceed.SUCCESS.getMessage());
            optLog.setMessage(StringUtils.EMPTY);
            optLog.setSource(StringUtils.isBlank(accessSource) ? StringUtils.EMPTY : accessSource);
            optLog.setRequestUrl(request.getRequestURI());
            optLog.setLogname(LOG_NAME_DESC);
            optLog.setLogtype(LogType.BUSSINESS.getMessage());
        }

        try {
            return proceedingJoinPoint.proceed();
        } catch (Exception e) {
            optLog.setLogtype(LogType.EXCEPTION.getMessage());
            optLog.setSucceed(LogSucceed.FAIL.getMessage());
            optLog.setMessage(e.getMessage());
            throw e;
        } finally {
            asyncAccessLog.logOpt(optLog);
        }


    }
}
